package impl;

import bean.Product;
import dao.ProductDao;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @classname:ProductDaoImpl
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/1 10:16
 * @version: 0.1
 * @since:1.8
 */
public class ProductDaoImpl implements ProductDao {
    private Connection conn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;
    private int result;

    //添加商品类型
    @Override
    public int addProduct(Product product) {
        conn = DBHelper.getCon();
        sql = "insert into product (parentId,typeName,is_parent,status) values (?,?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, product.getParentId());
            ps.setString(2, product.getTypeName());
            ps.setInt(3, product.getIs_parent());
            ps.setInt(4, product.getStatus());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps);
        }
        return result;
    }

    //展示全部商品类型信息
    @Override
    public List<Product> selectAll() {
        conn = DBHelper.getCon();
        List<Product> list = new ArrayList<>(10);
//        sql = "select * from product where parentId=?";
        sql = "select * from product";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Product(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps);
        }
        return list;
    }

    //通过id删除商品类型
    @Override
    public int deleteProductById(int pid) {
        conn = DBHelper.getCon();
        sql = "delete  from product where pid=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, pid);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps);
        }
        return result;
    }

    @Override
    public int updateProductById(Product product, int pid) {

        conn = DBHelper.getCon();
        sql = "update  product set parentId=?,typeName=?,is_parent=?,status=? where pid=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, product.getParentId());
            ps.setString(2, product.getTypeName());
            ps.setInt(3, product.getIs_parent());
            ps.setInt(4, product.getStatus());
            ps.setInt(5, pid);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps);
        }
        return result;
    }

    @Override
    public Product selectProductById(int id) {
        conn = DBHelper.getCon();
        sql = "select * from product where pid=?";
        Product product = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                product = new Product(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps, rs);
        }
        return product;
    }

    int count = 0;

    @Override
    public List<Product> selectProductByParams(Map<String, Object> params) {
        conn = DBHelper.getCon();
        List<Product> list = new ArrayList<>(10);
        StringBuilder builder = new StringBuilder("select * from product where ");
        int size = params.size();
        params.forEach((key, value) -> {
            count++;
            builder.append(key);
            builder.append("=");
            builder.append("'");
            builder.append(value);
            builder.append("'");
            if (count < size) {
                builder.append(" and ");
            }
        });

        try {
            ps = conn.prepareStatement(builder.toString());
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Product(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeResources(conn, ps, rs);
        }
        return list;
    }


}
